 var loginForm
Ext.onReady(function(){
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';
	 loginForm = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        frame:true,  
      	bodyStyle:'padding:5px;',
        labelAlign: 'right',
        defaults: {width: 230},
        defaultType: 'textfield',
        items: [{
                fieldLabel: '用户名',
                name: 'email',
                id:'email',
                allowBlank:false,
                vtype:'email'
            },{
                fieldLabel: '密码',
                name: 'passwd',
                id:'passwd',
                inputType:'password',
                allowBlank:false
            }
        ],

        buttons: [{
            text: '登录',
            handler:login
        },{
            text: '重置',
            type:'reset',
            handler:formreset
        },{
        	text:'注册',
        	handler:register
        }]
    });
	 var win = new Ext.Window({
		title: '用户登录',
		modal:true,
		draggable:false,
		closable:false,
        width: 380,
        height:150,
        layout: 'fit',
        plain:true,
        items:loginForm
	 }).show();
	 Ext.getCmp("passwd").on('specialkey',function(){
		var key = Ext.EventObject.getKey();
		if (key == 13){
			login();
		}
	 });
});

//这段代码是不使用ext的验证时自己写的检查代码，目前不用
function logintest(){
	alert('dfdf');
	var v_email = Ext.getDom("email").value;
	var v_pass = Ext.getDom("passwd").value;
	//验证Email地址是否正确

	var pattern = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	var result = pattern.test(v_email);
	if(v_email == ""){
		Ext.Msg.alert("提示","用户名不能为空",function(){
			Ext.getDom("email").focus();
		})
	}else if(!result){
		Ext.Msg.alert("提示","请输入正确的Email地址",function(){			
				Ext.getDom("email").value = "";
				Ext.getDom("email").focus();
			});
	}else if(v_pass == ""){
		Ext.Msg.alert("提示","密码不能为空!",function(){
			Ext.getDom("passwd").focus();
		});
	}else{
		Ext.Ajax.request({
		   url: "login.action",
		   success: okFn,
		   params:{email:v_email,passwd:v_pass}
		});

	}
}

function login(){
	var v_email = Ext.getDom("email").value;
	var v_pass = Ext.getDom("passwd").value;
	if(loginForm.getForm().isValid()){
		Ext.Ajax.request({
			url:"login!login.action",
			success:okFn,
			params:{email:v_email,passwd:v_pass}
		});
	}
}

function register(){
	location.href="register.jsp"
}

function formreset(){
		loginForm.getForm().reset();
	}

function okFn(obj){
	if(obj.responseText == "fail"){
		Ext.Msg.alert("提示","您输入的用户名或者密码有误,请重新输入!",function(){
				Ext.getDom("email").value = "";
				Ext.getDom("passwd").value = "";
				Ext.getDom("email").focus();
		});
	}else if(obj.responseText =="success"){
		location.href="../main/main.jsp"
	}else{
		location.href="../common/error.jsp"
	}
}